package com.primaryedu.ui;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.primaryedu.R;
import com.primaryedu.bean.AccountBean;
import com.primaryedu.data.Config;
import com.primaryedu.data.Contants;
import com.primaryedu.data.GlobalData;
import com.primaryedu.encrpy.EncrpyUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import okhttp3.Call;

/**
 * 登陆界面
 *
 * @author jun.wang
 * @description
 * @data 2016年12月12日
 */
public class LoginActivity extends BaseActivity implements OnClickListener {
    private TextView text_forget;
    SpannableString spannableString;
    private EditText edit_phone, edit_password;
    private PowerManager.WakeLock wakeLock;
    private DevicePolicyManager mDevicePolicyManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_login);
        init();
    }

    private void init() {
        text_forget = (TextView) this.findViewById(R.id.text_forget);
        spannableString = new SpannableString("忘记密码?");
        //设置下划线
        spannableString.setSpan(new UnderlineSpan(), 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        text_forget.setText(spannableString);
        findViewById(R.id.next).setOnClickListener(this);
        edit_phone = (EditText) this.findViewById(R.id.edit_phone);
        edit_password = (EditText) this.findViewById(R.id.edit_password);
        Config.towardIndex = 1;
    }

    @Override
    protected int BindLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.next:
                requestData();
                break;
            default:
                break;
        }
    }

    private void requestData() {
        if (TextUtils.isEmpty(edit_phone.getText().toString())) {
            showToast("请输入用户名!");
            return;
        }

        if (TextUtils.isEmpty(edit_phone.getText().toString())) {
            showToast("请输入密码!");
            return;
        }

        showDialog();
        Map<String, String> params = new HashMap<>();
        params.put("username", edit_phone.getText().toString());
        params.put("password", EncrpyUtils.getPhone(edit_password.getText().toString()));
        OkHttpUtils.post()//
                .url(Contants.login)
                .params(params)//
                .build()//
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(String response, int id) {
                        closeDialog();
                        System.out.println("responselogin==" + response);

                        try {
                            JSONObject json = new JSONObject(response);
//                            String reason = json.getString("reason");
                            int result = json.getInt("result");

                            if (result == 1) {
                                //测试屏幕开关
                                // 启用屏幕常亮功能
                                PowerManager pm =(PowerManager) getSystemService(POWER_SERVICE);
                                wakeLock= pm.newWakeLock(PowerManager.FULL_WAKE_LOCK| PowerManager.ACQUIRE_CAUSES_WAKEUP,
                                        "TAG");
                                wakeLock.acquire();
                                /*//关闭 屏幕常亮功能
                                if (wakeLock != null) {
                                    wakeLock.release();
                                }*/
//                                mDevicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
//                                mDevicePolicyManager.lockNow();
                                String data = json.getString("data");
                                AccountBean bean = new Gson().fromJson(response,
                                        new TypeToken<AccountBean>() {
                                        }.getType());
                                System.out.println("bean==" + bean);
                                GlobalData.accountBean = bean;
                                Log.i("yy", "className=======================>" + bean.getData().getOffice().getName());
                                Log.i("yy", "className=======================>" + bean.getData().getOffice().getId());
                                Log.i("yy", "UserType=======================>" + bean.getData().getUserType());
                                Log.i("yy", "classid=======================>" + bean.getData().getOffice().getId());
                                Log.i("yy", "serial=======================>" + bean.getData().getSerial());
                                GlobalData.saveUserId(LoginActivity.this, bean.getData().getId());
                                GlobalData.saveClassname(LoginActivity.this, bean.getData().getOffice().getName());
                                GlobalData.saveClassid(LoginActivity.this, bean.getData().getOffice().getId());
                                GlobalData.saveUsertype(LoginActivity.this, bean.getData().getUserType());
                                GlobalData.saveUsername(LoginActivity.this, bean.getData().getName());
                                GlobalData.saveUserNickname(LoginActivity.this, bean.getData().getNickname());
                                GlobalData.saveUserphotourl(LoginActivity.this, bean.getData().getPhotourl());
                                GlobalData.saveSerial(LoginActivity.this, bean.getData().getSerial());
                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                startActivity(intent);
                                finish();
                            } else {
                                showToast("用户名或密码错误！");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
       /* StringRequest stringRequest = new StringRequest(Request.Method.POST, Contants.login,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        closeDialog();
                        System.out.println("response==" + response);

                        try {
                            JSONObject json = new JSONObject(response);
                            String reason = json.getString("reason");
                            int result = json.getInt("result");

                            if (result == 1) {
                                String data = json.getString("data");
                                AccountBean bean = new Gson().fromJson(response,
                                        new TypeToken<AccountBean>() {
                                        }.getType());
                                System.out.println("bean==" + bean);
                                GlobalData.accountBean = bean;
                                Log.i("yy", "className=======================>" + bean.getData().getOffice().getName());
                                Log.i("yy", "className=======================>" + bean.getData().getOffice().getId());
                                Log.i("yy", "UserType=======================>" + bean.getData().getUserType());
                                Log.i("yy", "classid=======================>" + bean.getData().getOffice().getId());
                                GlobalData.saveUserId(LoginActivity.this, bean.getData().getId());
                                GlobalData.saveClassname(LoginActivity.this, bean.getData().getOffice().getName());
                                GlobalData.saveClassid(LoginActivity.this, bean.getData().getOffice().getId());
                                GlobalData.saveUsertype(LoginActivity.this, bean.getData().getUserType());
                                GlobalData.saveUsername(LoginActivity.this, bean.getData().getName());
                                GlobalData.saveUserNickname(LoginActivity.this, bean.getData().getNickname());
                                GlobalData.saveUserphotourl(LoginActivity.this, bean.getData().getPhotourl());
//						    	  GlobalData.saveNickname(LoginActivity.this, bean.getData().getNickname());
                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                startActivity(intent);
                            } else {
                                showToast("用户名或密码错误！");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                closeDialog();
                showToast("网络未连接或者错误！");
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                //在这里设置需要post的参数
                Map<String, String> map = new HashMap<String, String>();
                map.put("username", edit_phone.getText().toString());
                map.put("password", EncrpyUtils.getPhone(edit_password.getText().toString()));
                return map;
            }
        };
        requestQueue.add(stringRequest);*/
    }
}
